package cn.sxt.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.sys.domain.Role;
import cn.sxt.sys.mapper.RoleMapper;
import cn.sxt.sys.service.RoleService;
import cn.sxt.sys.utils.DataGridView;
import cn.sxt.sys.vo.RoleVo;

@Service
public class RoleServiceImpl implements RoleService{

	@Autowired
	private RoleMapper roleMapper;

	@Override
	public DataGridView queryAllRole(RoleVo roleVo) {
		Page<Object> page = PageHelper.startPage(roleVo.getPage(), roleVo.getLimit());
		List<Role> list = roleMapper.queryAllRole(roleVo);
		return new DataGridView(page.getTotal(), list);
	}

	@Override
	public int deleteRoleById(RoleVo roleVo) {
		// TODO Auto-generated method stub
		return roleMapper.deleteByPrimaryKey(roleVo.getId());
	}

	@Override
	public int addRole(RoleVo roleVo) {
		// TODO Auto-generated method stub
		return roleMapper.insert(roleVo);
	}

	@Override
	public int updateRole(RoleVo roleVo) {
		// TODO Auto-generated method stub
		return roleMapper.updateByPrimaryKeySelective(roleVo);
	}

	@Override
	public Role queryRoleById(Integer id) {
		// TODO Auto-generated method stub
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public int saveRolePermission(RoleVo roleVo) {
		Integer roleid = roleVo.getId();
		Integer[] ids = roleVo.getIds();
		//先删除之前的权限
		roleMapper.deleteRolePermissionByRoleId(roleid);
		if(ids!=null&&ids.length>0) {
			for (Integer pid : ids) {
				roleMapper.saveRolePermission(roleid,pid);
			}
		}
		return 1;
	}

	@Override
	public List<Role> queryRoleByUserId(Integer id) {
		// TODO Auto-generated method stub
		return roleMapper.queryRoleByUserId(id);
	}
}
